package com.lframework.xingyun.basedata.mappers;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.lframework.starter.web.core.mapper.BaseMapper;
import com.lframework.xingyun.basedata.bo.coupon.QueryCouponProductBo;
import com.lframework.xingyun.basedata.entity.CouponProduct;
import com.lframework.xingyun.basedata.entity.Product;
import com.lframework.xingyun.basedata.entity.ProductBrand;
import com.lframework.xingyun.basedata.entity.ProductCategory;
import com.lframework.xingyun.basedata.vo.coupon.QueryCouponProductVo;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * <p>
 * 卡券和商品关系表 Mapper 接口
 * </p>
 *
 */
@Mapper
public interface CouponProductMapper extends BaseMapper<CouponProduct> {

    /**
     * 查询列表
     * @param vo
     * @return
     */
    default IPage<QueryCouponProductBo> queryPage(QueryCouponProductVo vo) {
        return defaultJoinWrapper()
                .selectAll(CouponProduct.class)
                .selectAs(ProductCategory::getId, QueryCouponProductBo::getProductCategoryId)
                .selectAs(ProductCategory::getName, QueryCouponProductBo::getCategoryName)
                .selectAs(ProductBrand::getId, QueryCouponProductBo::getProductBrandId)
                .selectAs(ProductBrand::getName, QueryCouponProductBo::getBrandName)
                .selectAs(Product::getName, QueryCouponProductBo::getProductName)
                .selectAs(Product::getCode, QueryCouponProductBo::getProductCode)
                .selectAs(Product::getSkuCode, QueryCouponProductBo::getSkuCode)
                .selectAs(Product::getExternalCode, QueryCouponProductBo::getExternalCode)
                .selectAs(Product::getUnit, QueryCouponProductBo::getUnit)
                .selectAs(Product::getSpec, QueryCouponProductBo::getSpec)
                .leftJoin(Product.class, Product::getId, CouponProduct::getProductId)
                .leftJoin(ProductCategory.class, ProductCategory::getId, Product::getCategoryId)
                .leftJoin(ProductBrand.class, ProductBrand::getId, Product::getBrandId)
                .eq(StrUtil.isNotBlank(vo.getCouponId()), CouponProduct::getCouponId, vo.getCouponId())
                .and(StrUtil.isNotBlank(vo.getProductKeyword()), w -> {
                    w.like(Product::getName, vo.getProductKeyword())
                            .or()
                            .like(Product::getCode, vo.getProductKeyword())
                            .or()
                            .like(Product::getSkuCode, vo.getProductKeyword())
                            .or()
                            .like(ProductBrand::getShortName, vo.getProductKeyword());
                })
                .page(PageDTO.of(vo.getPageIndex(), vo.getPageSize()), QueryCouponProductBo.class);
    }

    /**
     * 查询列表
     * @param vo
     * @return
     */
    default List<QueryCouponProductBo> queryList(QueryCouponProductVo vo) {
        return defaultJoinWrapper()
                .selectAll(CouponProduct.class)
                .selectAs(ProductCategory::getId, QueryCouponProductBo::getProductCategoryId)
                .selectAs(ProductCategory::getName, QueryCouponProductBo::getCategoryName)
                .selectAs(ProductBrand::getId, QueryCouponProductBo::getProductBrandId)
                .selectAs(ProductBrand::getName, QueryCouponProductBo::getBrandName)
                .selectAs(Product::getName, QueryCouponProductBo::getProductName)
                .selectAs(Product::getCode, QueryCouponProductBo::getProductCode)
                .selectAs(Product::getSkuCode, QueryCouponProductBo::getSkuCode)
                .selectAs(Product::getExternalCode, QueryCouponProductBo::getExternalCode)
                .selectAs(Product::getUnit, QueryCouponProductBo::getUnit)
                .selectAs(Product::getSpec, QueryCouponProductBo::getSpec)
                .leftJoin(Product.class, Product::getId, CouponProduct::getProductId)
                .leftJoin(ProductCategory.class, ProductCategory::getId, Product::getCategoryId)
                .leftJoin(ProductBrand.class, ProductBrand::getId, Product::getBrandId)
                .eq(StrUtil.isNotBlank(vo.getCouponId()), CouponProduct::getCouponId, vo.getCouponId())
                .and(StrUtil.isNotBlank(vo.getProductKeyword()), w -> {
                    w.like(Product::getName, vo.getProductKeyword())
                            .or()
                            .like(Product::getCode, vo.getProductKeyword())
                            .or()
                            .like(Product::getSkuCode, vo.getProductKeyword())
                            .or()
                            .like(ProductBrand::getShortName, vo.getProductKeyword());
                })
                .list(QueryCouponProductBo.class);
    }
}

